//
// Created by XiaoXiu on 2025/5/17.
//
#ifndef SNAKEIO_H
#define SNAKEIO_H

#include <stdbool.h>
#include "snake.h"
#include <winsock2.h>

#pragma comment(lib, "ws2_32.lib")

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
  Packet *buffer[128];
  volatile int head; // 读位置
  volatile int tail; // 写位置
} PacketRingBuffer;


//资源操作
void closeClientSocket(SOCKET socketFD);

void initPacketRingbuffer(PacketRingBuffer *rb);

SOCKET initClientSocket(const char *host, const int port);

//缓冲
Packet *ringbuffer_pop(PacketRingBuffer *rb);

bool ringbuffer_push(PacketRingBuffer *rb, Packet *pkt);

bool ringbuffer_is_empty(const PacketRingBuffer *rb);

bool ringbuffer_is_full(const PacketRingBuffer *rb);

//发包
int sendPacket(SOCKET socketFD, const PacketClientSend *pkt);

//解析包
Packet *stringToPacket(const char *jsonStr);

//封装包
char *clientPacketToString(const PacketClientSend *pkt);

//工具
bool is_frame_packet(const Packet *pkt);

void sendPacketByType(PacketType type);

void sendDirPacket(Direction d);

#ifdef __cplusplus
}
#endif

#endif // SNAKEIO_H
